package com.css.fxfzdzzh.modules.basicData.landslideSite.controller;

import com.alibaba.fastjson.JSONObject;
import com.css.fxfzdzzh.base.response.RestResponse;
import com.css.fxfzdzzh.modules.basicData.landslideSite.entity.LandslideSiteMetaData;
import com.css.fxfzdzzh.modules.basicData.landslideSite.entity.LandslideSiteVersion;
import com.css.fxfzdzzh.modules.basicData.landslideSite.entity.LandslideSiteFileData;
import com.css.fxfzdzzh.modules.basicData.landslideSite.entity.param.DataVersionPageParam;
import com.css.fxfzdzzh.modules.basicData.landslideSite.service.LandslideSiteService;
import com.css.fxfzdzzh.modules.basicData.landslideSite.vo.DataPageVO;
import com.css.fxfzdzzh.modules.basicData.landslideSite.vo.MetaDataUpdateVO;
import com.css.fxfzdzzh.modules.basicData.landslideSite.vo.VersionDelVO;
import com.css.fxfzdzzh.modules.basicData.landslideSite.vo.VersionMetaDataInfoVO;
import com.css.fxfzdzzh.modules.basicData.param.SysDataVersionSameTypeDataExistParam;
import com.css.fxfzdzzh.modules.evaluate.VO.VersionPageVO;
import com.css.fxfzdzzh.util.PlatformObjectUtils;
import com.css.fxfzdzzh.util.ResponseCreator;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Map;
import java.util.concurrent.ExecutionException;

/**
 * @author Dbj
 * @date 2023年02月20日  16:45
 * 滑坡现场逻辑
 */
@RestController
@RequestMapping("/landslideSite")
@Validated
public class LandslideSiteController {

    @Resource
    LandslideSiteService landslideSiteService;

    /**
     * 分页条件查询
     * @param dataVersionPageParam
     * @return
     */
    @GetMapping("/page")
    public RestResponse page(DataVersionPageParam dataVersionPageParam) {
        return ResponseCreator.createSuccessResponse(landslideSiteService.page(dataVersionPageParam));
    }

    /**
     * 保存新版本
     */
    @PostMapping("saveNewVersion")
    public RestResponse saveNewVersion(@Valid @RequestBody LandslideSiteVersion landslideSite) {
        String versionCode = landslideSiteService.saveNewVersion(landslideSite);
        return ResponseCreator.createSuccessResponse(versionCode,"新增成功");
    }

    /**
     * 生成新的版本编号  点击新增页面时调用生成版本号保存在页面，上传数据和保存版本时使用并传递
     */
    @GetMapping("/getVersionCode")
    public RestResponse getVersionCode(@NotBlank(message = "数据级别不能为空")String dataLevel){
        return ResponseCreator.createSuccessResponse(landslideSiteService.getVersionCode(dataLevel));
    }


    /**
     * 覆盖旧版本
     */
    @PostMapping("overWriteOldVersion")
    public RestResponse overWriteOldVersion(@RequestBody LandslideSiteVersion sysDataVersion) {
        String versionCode = landslideSiteService.overWriteOldVersion(sysDataVersion);
        return ResponseCreator.createSuccessResponse(versionCode,"覆盖旧版本成功");
    }


    /**
     * 元数据新增页面数据显示
     */
    @GetMapping("/getVersionMetaDataInfo")
    public RestResponse getVersionMetaDataInfo(@NotBlank(message = "版本code不能为空")  String versionCode) {
        VersionMetaDataInfoVO vo = landslideSiteService.getVersionMetaDataInfo(versionCode);
        return ResponseCreator.createSuccessResponse(vo);
    }

    /**
     * 新增元数据信息
     */
    @PostMapping("/saveMetaDataInfo")
    public RestResponse saveMetaDataInfo(@Valid @RequestBody LandslideSiteMetaData metaData) {
        landslideSiteService.saveMetaDataInfo(metaData);
        return ResponseCreator.createSuccessResponse("元数据保存成功");
    }

    /**
     * 修改元数据信息
     */
    @PostMapping("/updateMetaDataInfo")
    public RestResponse updateMetaDataInfo(@Valid @RequestBody LandslideSiteMetaData metaData) throws Exception{
        landslideSiteService.updateMetaDataInfo(metaData);
        return ResponseCreator.createSuccessResponse("元数据修改成功");
    }

    /**
     * 删除版本信息
     */
    @PostMapping("/delete")
    public RestResponse delete(@RequestBody VersionDelVO vo) throws Exception {
        if(vo.getCodeList().size()==0){
            return ResponseCreator.createFailResponse("请选择要删除的数据");
        }
        landslideSiteService.delete(vo.getCodeList());
        return ResponseCreator.createSuccessResponse("删除完成");
    }

    /**
     * 数据文件解析
     */
    @GetMapping("/parseShp")
    public void parseShp(String fileIds,String versionCode) throws Exception{
        landslideSiteService.parseShp(fileIds, versionCode);
    }

    /**
     * 分页条件查询
     * @param dataPageVO
     * @return
     */
    @GetMapping("/dataPage")
    public RestResponse dataPage(DataPageVO dataPageVO) {
        return ResponseCreator.createSuccessResponse(landslideSiteService.dataPage(dataPageVO));
    }

    /**
     * 导出
     * @param dataPageVO
     * @return
     */
    @PostMapping("/exportExcel")
    public void exportExcel(HttpServletResponse response, @RequestBody DataPageVO dataPageVO) throws Exception {
        landslideSiteService.exportExcel(response,dataPageVO);
    }

    /**
     * 字段说明
     * @param
     * @return
     */
    @GetMapping("/fieldDesc")
    public RestResponse fieldDesc() {
        return ResponseCreator.createSuccessResponse(landslideSiteService.getFieldDesc());
    }

    /**
     * 分页条件查询
     * @param param
     * @return
     */
    @GetMapping("/pageForAnalyse")
    public RestResponse pageForAnalyse(VersionPageVO param) {
        return ResponseCreator.createSuccessResponse(landslideSiteService.pageForAnalyse(param));
    }

    /**
     * 判断同类别数据是否已经存在
     * */
    @GetMapping("/sameTypeDataExist")
    public RestResponse sameTypeDataExist(SysDataVersionSameTypeDataExistParam sysDataVersionSameTypeDataExistParam) {
        try {
            return RestResponse.succeed(landslideSiteService.sameTypeDataExist(sysDataVersionSameTypeDataExistParam));
        } catch (Exception e) {
            return RestResponse.fail("查询失败!");
        }
    }

    /*
     * 获取版本列表
     * */
    @GetMapping("/getVersionCodeList")
    public RestResponse getVersionCodeListByType() {
        return RestResponse.succeed(landslideSiteService.getVersionCodeListByType());
    }

    @PostMapping(value = "/downloadExcel", produces = "text/html;charset=UTF-8")
    public void downloadExcel1( HttpServletResponse response) {
        String fileName = "滑坡现场调查数据导入模板.xlsx";
        try {
            //请求头的模板名为中文
            String file_name = URLEncoder.encode(fileName, "UTF-8");
            response.setHeader("content-Type", "application/vnd.ms-excel");
            response.setHeader("Content-disposition", "attachment;fileName=" + file_name);
//            response.setContentType("application/x-download");
            //获取文件的路径
            //String path = getClass().getResource("/template/" + fileName).getPath();
            String path = "/usr/zhaoshuai/dzzh/template" + "/" + file_name;
            //路径名转换中文
            String filePath = URLDecoder.decode(path, "utf-8");
            FileInputStream inputStream = new FileInputStream(filePath);
            OutputStream outputStream = response.getOutputStream();
            byte[] bytes = new byte[65536];
            int len;
            while ((len = inputStream.read(bytes)) != -1) {
                outputStream.write(bytes, 0, len);
            }
            outputStream.flush();
            outputStream.close();
            inputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 新增或替换版本页面上传了数据文件随后点击删除数据文件
     * @param map
     * @return
     */
    @PostMapping("/delDataFile")
    public void delDataFile(@RequestBody Map<String,String> map){
        String versionCode = map.get("versionCode");
        if(StringUtils.isNotBlank(versionCode)){
            String fileId = map.get("fileId");
            landslideSiteService.delDataFile(versionCode,fileId);
        }
    }

    @GetMapping("/listForDp")
    public RestResponse listForDp() throws ExecutionException, InterruptedException {
        JSONObject js = landslideSiteService.listForDp();
        return ResponseCreator.createSuccessResponse(js);
    }
}
